var db = require('./db.js');

function Order(order) {
    this.id = order.id;
    this.userId = order.userId;
    this.productId = order.productId;
    this.num = order.num;


};

// INSERT INTO `oder` (`id`, `productId`, `userId`, `num`) VALUES (null, '5', '5', '5'), (null, '5', '5', '5');
Order.addOrder = function(order, callback) {

    var selectSql = `INSERT INTO oder (id,userId,productId,num) VALUES `;
    let arr = order.num.toString().split(',');


    for (let i = 0; i < order.productId.length; i++) {
        if (arr.length !== 1) {
            selectSql += `(null,${order.userId},${parseInt(order.productId[i])},${parseInt(arr[i])})`;
        } else {
            selectSql += `(null,${order.userId},${parseInt(order.productId[i])},${parseInt(order.num)})`;
        }
        if (i != order.productId.length - 1) {
            selectSql += ",";
        } else {
            selectSql += ";"
        }

    }
    console.log(selectSql);

    db.query(selectSql, function(err, result) {
        if (err) {
            return callback(err);
        }
        callback(err, result);
    });
};
// Cart.updateCart = function(cart, callback) {
//     var selectSql = 'UPDATE cart SET num =?,size=? WHERE id=?';
//     db.query(selectSql, [cart.num, cart.size, cart.id], function(err, res) {
//         console.log(res);
//         if (err) {
//             console.log(err);
//             return callback(err);
//         }
//         callback(err, res);
//     });
// }
Order.deleteOrder = function(id, callback) {


    var delSql = "DELETE FROM oder WHERE  userId= " + id;
    console.log(delSql);
    // var delSql = "UPDATE cart SET isDelete =0 WHERE id = " + id;



    db.query(delSql, function(err, res) {
        if (err) {
            return callback(err);
        }
        callback(err, res);
    });
}

Order.queryOrder = function(id, callback) {
    var selectSql = `SELECT c.id,c.productId,c.num,p.proName,p.price,p.num as productNum from oder as c left join product as p on c.productId=p.id where c.userId= ${id}`;
    console.log(selectSql)
    db.query(selectSql, function(err, res) {
        if (err) {

            return callback(err);
        }
        callback(err, res);
    });
}


// Cart.queryCartPaging = function(id, page, callback) {
//     var selectSql = 'SELECT c.id,c.productId,c.num,c.size,p.proName,p.price,p.oldPrice,p.num as productNum,p.statu,p.size as productSize from cart as c left join product as p on c.productId=p.id where c.isDelete = 1 and c.userId=?';

//     selectSql += " LIMIT ?,?";
//     db.query(selectSql, [id, (page.page - 1) * page.size, page.size], function(err, res) {
//         if (err) {
//             return callback(err);
//         }
//         callback(err, res);
//     });
// }
// Cart.queryCart = function(id, callback) {
//     var selectSql = 'SELECT c.id,c.productId,c.num,c.size,p.proName,p.price,p.oldPrice,p.num as productNum,p.statu,p.size as productSize from cart as c left join product as p on c.productId=p.id where c.userId=? and c.isDelete = 1';
//     console.log(id)
//     db.query(selectSql, [id], function(err, res) {
//         if (err) {

//             return callback(err);
//         }
//         callback(err, res);
//     });
// }
// Cart.countCart = function(id, callback) {
//     var delSql = 'SELECT count(c.id) as count from cart as c left join product as p on c.productId=p.id where  c.isDelete = 1 and  c.userId=?';
//     db.query(delSql, [id], function(err, res) {
//         if (err) {
//             return callback(err);
//         }
//         callback(err, res[0]);
//     });
// }
module.exports = Order;